package org.luxor.commons.security.config.admin;

import org.luxor.commons.security.annotation.EnableAdminServer;
import org.luxor.commons.security.config.DefaultWebSecurityConfigurerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Map;


/**
 * EnableAdminSecurity 注解配置注册器
 *
 * @author lengleng @date 2019/03/08
 */
public class AdminServerBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    private static final Logger log = LoggerFactory.getLogger(AdminServerBeanDefinitionRegistrar.class);

    /**
     * 根据注解值动态注入资源服务器的相关属性
     *
     * @param metadata 注解信息
     * @param registry 注册器
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
        Map<String, Object> defaultAttrs = metadata
                .getAnnotationAttributes(EnableAdminServer.class.getName(), true);

        if (defaultAttrs == null) {
            return;
        }

        log.info("load AdminServerConfigurer Started...");

        GenericBeanDefinition beanDefinitionOne = new GenericBeanDefinition();
        beanDefinitionOne.setBeanClass(DefaultWebSecurityConfigurerAdapter.class);
        registry.registerBeanDefinition("defaultWebSecurityConfiguration", beanDefinitionOne);

        GenericBeanDefinition beanDefinitionTwo = new GenericBeanDefinition();
        beanDefinitionTwo.setBeanClass(AdminServerConfigurer.class);
        registry.registerBeanDefinition("adminServerConfigurer", beanDefinitionTwo);

        log.info("load AdminServerConfigurer completed.");
    }

}